//
// Created by lingwu on 2021/5/11.
//
#include "iostream"
#ifndef DEMO_BTREE_H
#define DEMO_BTREE_H

#endif //DEMO_BTREE_H
typedef char ElemType;
#define MaxSize 50
typedef struct node{
//    数据结点类型
    ElemType data;
    struct node *lchild;
    struct  node *rchild;
}BTNode;
void CreateBTree(BTNode *&b,char *str){
//    创建二叉树函数
    BTNode *St[MaxSize],*p;
    int top=-1,k,j=0;
    char ch;
    b=NULL;
    ch=str[j];
    while(ch!='\0'){
        switch (ch) {
            case '(':top++;St[top]=p;k=1;break;
            case ')':top--;break;
            case ',':k=2;break;
            default:p= (BTNode *)malloc(sizeof(BTNode));
            p->data=ch;
            p->lchild=p->rchild=NULL;
                if (b==NULL)
                    b=p;
                else{
                    switch (k) {
                        case 1:St[top]->lchild=p;break;
                        case 2:St[top]->rchild=p;break;

                    }
                }

        }
        j++;
        ch=str[j];
    }
}
void DestroyBTree(BTNode *&b){
//    销毁二叉树
    if (b!=NULL){
        DestroyBTree(b->lchild);
        DestroyBTree(b->rchild);
        free(b);
    }
}
void DisBTree(BTNode *b){
//    显示二叉树
    if (b!=NULL){
        printf("%c",b->data);
        if (b->rchild!=NULL||b->lchild!=NULL){
            printf("(");
            DisBTree(b->lchild);
            if (b->rchild!=NULL)
                printf(",");
            DisBTree(b->rchild);
            printf(")");
        }
    }
}